import requests
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import scrolledtext
from datetime import datetime
import time
import pickle
import os
import json
import subprocess
import ctypes
import configparser

list = [
    {
        "sessionid": "d121fc9bbd276facd9d6e1ae",
        "steamLoginSecure": "76561198368032227%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1NV8yNTAxMzI3NV9DNjVERSIsICJzdWIiOiAiNzY1NjExOTgzNjgwMzIyMjciLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU4ODIxOTgsICJuYmYiOiAxNzE3MTU1NDUxLCAiaWF0IjogMTcyNTc5NTQ1MSwgImp0aSI6ICIxMDYzXzI1MDEzMjc1X0JGQ0RDIiwgIm9hdCI6IDE3MjU3OTU0NTEsICJydF9leHAiOiAxNzQzNzk2OTY3LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTg1LjIwNi4yNDkuMjEiLCAiaXBfY29uZmlybWVyIjogIjMuMTEyLjE5OC4xNDEiIH0.ZH0x7n5N-a7e3gmcsZIdABK3omNZCDwRy5BlPXZdEmJXfypnjP1m-hWBHac_UGG27jdjxGpqezPnMQv1WfRrDg",
    },
    {
        "sessionid": "0606d94ec1330019392e8e68",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Ml8yNTAxMzI4M183QTcyRCIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTAyNzgsICJuYmYiOiAxNzE3MjIyNjUyLCAiaWF0IjogMTcyNTg2MjY1MiwgImp0aSI6ICIxMDUzXzI1MDEzMjgzXzFFNTc5IiwgIm9hdCI6IDE3MjU4NjI2NTIsICJydF9leHAiOiAxNzQzOTY3NjUwLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEwOS4xMTAuMTYyLjMyIiB9.am44l86EVt2GqhHixxJppF_3NIL_-W3W7AQN_8AnI10jnsLANeW4Wv5OezvWMsu_lUKWGsTUZwGnwDS3JBUWCw",
    },
    {
        "sessionid": "8b00b7f8d3c4e36ec1ca6fa5",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1M18yNTAxMzI3NV9CN0Q3NSIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU4ODI3MTEsICJuYmYiOiAxNzE3MTU1NjI0LCAiaWF0IjogMTcyNTc5NTYyNCwgImp0aSI6ICIxMDUzXzI1MDEzMjc1X0I5REI1IiwgIm9hdCI6IDE3MjU3OTU2MjQsICJydF9leHAiOiAxNzQzNzY5MTY4LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTg1LjIwNi4yNDkuMjEiLCAiaXBfY29uZmlybWVyIjogIjE4NS4yMDYuMjQ5LjIxIiB9.09ShWaIwJdSIJ6KPfdWQFbhykMBVNs90RuNoKcA7p6Jt7kvIyBnTXGOyWWFptlp2VUz69PV26DM89ZqlwFzpBg",
    },
    {
        "sessionid": "dd3455dcc22c1a031709bcba",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1OF8yNTAxMzI4RF81RkU4MiIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTA5MjYsICJuYmYiOiAxNzE3MjIzODQ2LCAiaWF0IjogMTcyNTg2Mzg0NiwgImp0aSI6ICIxMDUzXzI1MDEzMjgzXzVBOTk4IiwgIm9hdCI6IDE3MjU4NjM4NDUsICJydF9leHAiOiAxNzQzNzkxMjU5LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEwOS4xMTAuMTYyLjMyIiB9.tcM1UtYEQaLZJfh5rnROzRsObyeqbmE-SBT4gV3QCbj1qpuSCgMc8C4GfvsyrrHrU8-1SLNeWW-hcBM4hTulCA",
    },
    {
        "sessionid": "d395e019f66875aa415ab799",
        "steamLoginSecure": "76561198368032227%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1NF8yNTAxMzI4M19DQTg4OSIsICJzdWIiOiAiNzY1NjExOTgzNjgwMzIyMjciLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTQ2NDIsICJuYmYiOiAxNzE3MjI2OTU1LCAiaWF0IjogMTcyNTg2Njk1NSwgImp0aSI6ICIxMDYzXzI1MDEzMjg0XzBFRUEzIiwgIm9hdCI6IDE3MjU4NjY5NTQsICJydF9leHAiOiAxNzQzNzIzNjk4LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEzLjExMi4xNDQuMjUxIiB9.gLarTXVbicHCY6LQ06Ez0e_-DgnG9531ce818FBg_v2UI34QYPRiYrKFzuP0f7PA6GmEiytmAePQ-nP_5dl9AA",
    },
    {
        "sessionid": "41d4ef16445e61948a652718",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Rl8yNTAxMzI4M180RjY2QSIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTI0MTcsICJuYmYiOiAxNzE3MjI0ODQ5LCAiaWF0IjogMTcyNTg2NDg0OSwgImp0aSI6ICIxMDUzXzI1MDEzMjgzXzhDN0NCIiwgIm9hdCI6IDE3MjU4NjQ4NDksICJydF9leHAiOiAxNzQzNzMxNTIzLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEwOS4xMTAuMTYyLjMyIiB9.c7H9hN9PpeZFr_eid2RhNuTakXVe9HvbXQBp1WDadiHc5PIPBOSg7HTb2dXK_sQS3bf7i93oBECF_7pkrV2wCA",
    },
    {
        "sessionid": "0853a77f7766b2feaf6fe6ed",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1MV8yNTAxMzI4M185RkZCNyIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTI0NjQsICJuYmYiOiAxNzE3MjI1NzkzLCAiaWF0IjogMTcyNTg2NTc5MywgImp0aSI6ICIxMDUzXzI1MDEzMjgzX0JEQTBDIiwgIm9hdCI6IDE3MjU4NjU3OTMsICJydF9leHAiOiAxNzQzOTQwNDc3LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEwOS4xMTAuMTYyLjMyIiB9._tVRs9Ufi6bMC4-eeiR9u9baq1NeOT7ahZCxhk1g-HW-yEuiH7Fygv9eBIpWsWVxLs_hpo_to3gE3HR1cJJ4CQ",
    },
    {
        "sessionid": "4894fd7d9cd59a197ba18b4d",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Ml8yNTAxMzI4NF81MkRDMyIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTQ2MDksICJuYmYiOiAxNzE3MjI2ODYzLCAiaWF0IjogMTcyNTg2Njg2MywgImp0aSI6ICIxMDUzXzI1MDEzMjgzX0Y1QkQ2IiwgIm9hdCI6IDE3MjU4NjY4NjIsICJydF9leHAiOiAxNzQ0MTY4MzYyLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEwOS4xMTAuMTYyLjMyIiB9.McUBdB7DXdu6xZUguK4Mjo9yOzRZvayDtBIczYgCXIS2VKRxa-_2WfI58v8yeFycROxr3LtGXSvAGQo98Ex2AA",
    },
    {
        "sessionid": "279e9eeb63b557752940bb0b",
        "steamLoginSecure": "76561198368032227%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Ql8yNTAxMzI4NF8wRTk5MiIsICJzdWIiOiAiNzY1NjExOTgzNjgwMzIyMjciLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NTM1NTksICJuYmYiOiAxNzE3MjI3MDM3LCAiaWF0IjogMTcyNTg2NzAzNywgImp0aSI6ICIxMDYzXzI1MDEzMjg0XzEyQzAwIiwgIm9hdCI6IDE3MjU4NjcwMzcsICJydF9leHAiOiAxNzQzODQ2MTExLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEzLjExMi4xNDQuMjUxIiB9.JCGq4h0f8_kD4Sh4ZrN4y7sn6vOG7H6tRsZSOc-yJuYqh4qFZTsvLtDdosrzHv_nAvAkymz3ijv7lhmbVDNNCQ",
    },
    {
        "sessionid": "d16edeb709a92ea76e0a6d16",
        "steamLoginSecure": "76561199100985210%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA2Nl8yNTAxMzI1OV8wRDgzRiIsICJzdWIiOiAiNzY1NjExOTkxMDA5ODUyMTAiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjU5NDE3ODQsICJuYmYiOiAxNzE3MjEzNzg5LCAiaWF0IjogMTcyNTg1Mzc4OSwgImp0aSI6ICIxMDUzXzI1MDEzMjgxXzY5MDlDIiwgIm9hdCI6IDE3MjU2NTA0MTYsICJydF9leHAiOiAxNzQzOTUxNzY0LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA5LjExMC4xNjIuMzIiLCAiaXBfY29uZmlybWVyIjogIjEwOS4xMTAuMTYyLjMyIiB9.rUJlre8hNkYJB7krIycsOdVjz4xMLX361UwowjDuJJ9z27PKabezeqcfSXSGLVdNF5S1OLRhar_stsmxEzSVBw",
    },
]

index = 0

user_id = ""
api_url = ""


def get_urldata():
    global index
    try:
        current_credentials = list[index]
        sessionid = current_credentials["sessionid"]
        steamLoginSecure = current_credentials["steamLoginSecure"]
        response = requests.get(api_url)
        soup = BeautifulSoup(response.text, "html.parser")
        element = soup.select_one(".profile_header_size")
        if element:
            miniprofile_value = element.get("data-miniprofile")
            global user_id
            user_id = miniprofile_value
            # print(miniprofile_value)
        else:
            print("未找到指定 class 的元素。")
            return
        if response.status_code == 200:
            headers = response.headers
            # print(f"headers {headers}")
            # 请求第二个接口
            get_steam_data(headers, miniprofile_value, sessionid, steamLoginSecure)
            return response.json()
        else:
            print(f"请求失败，状态码：{response.status_code}")
            # print_info(f"请求失败，状态码：{response.status_code}")
            return None
    except Exception as e:
        # print(f"发生错误：{e}")
        print(f"请求中...")
        # print_info(f"发生错误：{e}")
        return None


def get_steam_data(headers_from_first, miniprofile_value, sessionid, steamLoginSecure):
    try:
        timestamp = int(time.time())  # 获取当前时间戳
        # api_url = f"https://steamcommunity.com/miniprofile/{miniprofile_value}/json"
        api_url = f"https://steam-chat.com/miniprofile/{miniprofile_value}/json/?origin=https://steamloopback.hos?timestamp={timestamp}"
        headers = {
            "Cookie": f"cookie=sessionid={sessionid};steamLoginSecure={steamLoginSecure}",
            "User-Agent": "Apifox/1.0.0 (https://apifox.com)",
            "Accept": "*/*",
            "Host": "steam-chat.com",
            # 'Connection': 'keep-alive'
            "Connection": "close",
            "Cache-Control": "no-cache, no-store, must-revalidate",
            "Pragma": "no-cache",
            "Expires": "0",
        }
        print("sessionid----------------------------", sessionid)

        # print("api_url----------------------------",api_url)
        # print("headers",headers)
        response = requests.get(api_url, headers=headers)
        if response.status_code == 200:
            data = response.json()
            if "in_game" in data and "rich_presence" in data["in_game"]:
                rich_presence_value = data["in_game"]["rich_presence"]
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                print(
                    "当前时间:" f"{current_time}: 地图及比分: {rich_presence_value}\n"
                )
            else:
                print("找不到 'rich_presence' 的值。")
            return response.json()
        else:
            print(f"请求失败，状态码：{response.status_code}")
            # print_info(f"请求失败，状态码：{response.status_code}")
            return None
    except Exception as e:
        # print(f"发生错误：{e}")
        print(f"请求中...")
        return None


print("请输入目标网址，按回车开始")
input_url = input()
api_url = input_url
get_urldata()

while True:
    time.sleep(6)
    if user_id:
        index = (index + 1) % len(list)
        print("当前index", index)
        get_steam_data(
            None, user_id, list[index]["sessionid"], list[index]["steamLoginSecure"]
        )
    else:
        print("用户 ID 为空，无法继续请求。")
